<template>
  <div class="apply-back-dialog">
    <MyDialog
      :show="isApplyBack"
      customName="apply-back-dialog"
      dialogWidth="600px"
      @beforeClose="beforeClose"
    >
      <template #body>
        <el-form
          :rules="rulesForm"
          :model="backForm"
          label-width="110px"
          size="small"
          ref="applyBackForm"
        >
          <el-table
            :data="selectedBoxList"
            class="box-table"
            size="mini"
            height="200"
            border
          >
            <el-table-column prop="date" label="窍编号" width="180" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.boxCode }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="窍类型" align="center">
              <template slot-scope="scope">
                <span>{{commonFun.getLabel(boxType, scope.row.boxType)}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="removeItem(scope.row)"
                >
                  移除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
            <el-col :span="20" class="address-list">
               <el-form-item prop="addressId" label="选择地址">
                  <el-select
                    v-model="backForm.addressId"
                    placeholder="请选择地址"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in addressList"
                      :key="item.id"
                      :label="item.fullArea+item.address + ' ' + item.mobile + ' ' + item.fullName"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
            </el-col>

            <el-col :span="4">
              <div class="add-address-btu">
                <el-button type="text" @click="showAddAddress" size="mini">
                  <i class="el-icon-plus"></i>
                  添加地址</el-button
                >
              </div>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item prop="deliveryOn">
                <span slot="label" class="delivery-label">
                  <span>回库日期</span>
                  <el-popover placement="top" width="250" trigger="hover">
                    <div class="on-time">
                      <p>请至少提前一天预约</p>
                      <div>
                        如无特殊情况，当天原件回库需要支付额外的加急费用。客服将很快和您确认时间，请保持手机畅通。
                      </div>
                    </div>

                    <div slot="reference" class="popover-reference">
                      <el-button type="text">
                        <i class="el-icon-question"></i>
                      </el-button>
                    </div>
                  </el-popover>
                </span>

                <el-date-picker
                  @change="changeDatePicker"
                  v-model="backForm.deliveryOn"
                  type="date"
                  placeholder="选择回库日期"
                  :clearable="false"
                  :picker-options="forbiddenTime"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
             <el-col :span="5" :offset='1'>
              <el-popover placement="top" width="250" trigger="hover">
                <div class="on-time">
                  <p>准时服务承诺</p>
                  <div>
                    当您下单选择了窍楚的服务， 我们承诺在您选择的时间内送达
                  </div>
                  <div>
                    若由于窍楚原因未能准时送达， 您可通过’在线客服‘提交申请，
                    经客服审核确认后， 将按照您支付的配送费返利至您的账户
                  </div>
                </div>

                <span slot="reference" class="popover-reference">
                  <el-button type="text">
                    <i class="el-icon-present"></i>准时服务承诺
                  </el-button>
                </span>
              </el-popover>
            </el-col>
            <el-col :span="5">
              <el-button type="text" @click="toSeeCost">
                <i class="el-icon-search"></i>物流费用标准
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <div slot="title">
        <div class="dialog-title">申请回库</div>
      </div>
      <div slot="footer">
        <span>
          <el-button size="small" @click="applyClose">取 消</el-button>
          <el-button
            size="small"
            type="primary"
            :loading="btnLoading"
            @click="backConfirm('applyBackForm')"
            >提交</el-button
          >
        </span>
      </div>
    </MyDialog>

    <AddressEdit
      :isShowAddAddress="isShowAddAddress"
      title="添加地址"
      @closeDialog="closeAddressDialog"
    />

    <LogisticsCost
      title="物流上门收费标准"
      :isShowCost="isShowCost"
      @closeDialog="closeCostDialog"
    />

    <!-- 当天日期加急提示 -->
    <MyDialog
      :show="isTipShow"
      @beforeClose="isTipShow = false"
      title="加急提示"
    >
      <template #body>
        <div class="tip-box">
          <div class="content">
            尊敬的客户您好，您现在选择的是当天加急服务，当天加急需要支付额外的加急费用
            <!-- <span class="color-red">（50元起）</span> -->
            。如需限时加急服务，欢迎致电400-877-0039。
          </div>
        </div>
      </template>
    </MyDialog>
  </div>
</template>

<script>
import { boxType } from "@/utils/dictionary.js";
import { encryption, applySubType, clarityType } from "@/utils/dictionary.js";
import { toApplyBorrow } from "@/api/access-manage/index";
import { getAddressList } from "@/api/member/address";
import AddressEdit from "../../access-manage/components/AddressEdit";
import LogisticsCost from "../../access-manage/components/LogisticsCost";
export default {
  name: "ApplyBackDialog",
  props: {
    isApplyBack: {
      type: Boolean,
      default: false,
    },
    userInfo: {
      type: Object,
      default: () => {},
    },
    selectedList: {
      type: Array,
      default: () => [],
    },
  },
  components: {
    AddressEdit,
    LogisticsCost,
  },
  watch: {
    isApplyBack() {
      if (this.isApplyBack) {
        this.selectedBoxList = this.selectedList
        this.getAddressList();
        this.backForm.deliveryOn = Date.now() + 8.64e7;
      } else {
        this.backForm = {
          addressId: null,
          deliveryOn: undefined,
        };
      }
    },
  },
  data() {
    return {
      selectedBoxList: [],
      isTipShow: false,
      boxType: boxType,
      forbiddenTime: {
        //禁用当前日期之前的日期
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      isShowMap: false,
      isShowCost: false,
      btnLoading: false,
      fileList: [],

      uploadFileList: [],
      isShowAddAddress: false,
      addressList: [],
      isexpand: false,
      loopForm: {},
      encryption: encryption,
      applySubType: applySubType,
      clarityType: clarityType,
      backForm: {
        addressId: null,
        deliveryOn: Date.now() + 8.64e7,
      },
      rulesForm: {
        addressId: [{ required: true, message: "请选择地址" }],
        deliveryOn: [{ required: true, message: "请选择回库日期" }],
      },
    };
  },

  methods: {
    // 回库时间改变
    changeDatePicker(value) {
      if (
        new Date(value).toLocaleDateString() === new Date().toLocaleDateString()
      ) {
        // 当天日期 跳出提示弹窗
        this.isTipShow = true;
      }
    },
    // 移除窍
    removeItem(row) {
      this.selectedBoxList = this.selectedBoxList.filter((e) => e.userBoxId != row.userBoxId);
    },
    // 新增地址弹窗
    closeAddressDialog() {
      this.isShowAddAddress = false;
      this.getAddressList();
    },

    // 查看物流费
    toSeeCost() {
      this.isShowCost = true;
    },
    //关闭物流费弹窗
    closeCostDialog() {
      this.isShowCost = false;
    },

    // 展开添加地址弹窗
    showAddAddress() {
      this.isShowAddAddress = true;
    },
    // 获得地址列表
    getAddressList() {
      getAddressList().then((res) => {
        this.addressList = res.data;
        this.backForm.addressId = res.data.filter(e => e.isDefault == 1)[0].id
      });
    },

    // 关闭回库窗口
    beforeClose() {
      this.$emit("closeApplyDialog");
    },
    // 取消按钮 - 关闭表单
    applyClose() {
      this.$emit("closeApplyDialog");
    },

    //申请回库 表单提交
    backConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.selectedBoxList.length === 0) {
            this.$message({
              type: "error",
              message: "请选择窍后再申请回库",
            });
            return;
          }
          this.btnLoading = true;

          let userBoxList = [];
          this.selectedBoxList.map((e) => {
            userBoxList.push({ userBoxId: e.userBoxId, boxCode: e.boxCode, userId: e.userId });
          });

          let addressInfo = {};

          addressInfo.deliveryOn =
            this.$moment(this.backForm.deliveryOn).valueOf() / 1000;

          // // 有地址
          let selected = this.addressList.filter(m => m.id === this.backForm.addressId)[0];

          addressInfo.address = selected.fullArea + selected.address;

          addressInfo.contactName = selected.fullName;
          addressInfo.contactMobile = selected.mobile;
          const data = {
            // type: 1, // 回库
            subType: 2, // 存窍订单
            ...this.backForm,
            userBoxList: userBoxList,
            ...addressInfo,
          };

          new Promise((resolve) => {
            toApplyBorrow(data)
              .then((res) => {
                this.btnLoading = false;
                this.$message({
                  type: "success",
                  message: "申请成功",
                });
                this.$emit("closeApplyDialog");
                this.$emit("getList");

                // 跳转至回库列表
                this.$router.push({ name: "backLogList" });
              })
              .catch((err) => {
                this.btnLoading = false;
                this.$emit("closeApplyDialog");
              });
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.apply-back-dialog {
  .dialog-title {
    color: #f88123;
    font-size: 16px;
  }
  .box-table {
    width: 90%;
    margin: 0 auto;
    margin-bottom: 20px;
  }


  .more-box-show {
    background: #eee;
    color: #f88123;
    padding-left: 10px;

    .selected-list {
      max-height: 200px;
      overflow-y: scroll;
    }
  }
  .show-more {
    color: #f88123;
    padding-left: 18px;
    margin-bottom: 10px;
    line-height: 30px;
    .more {
      width: 120px;
      cursor: pointer;
    }
  }
}

.address-list {
  ::v-deep  .el-radio-group {
    display: flex;
    flex-direction: column;
    height: 100px;
    overflow-y: scroll;
    // 不展示滚动条
    &::-webkit-scrollbar {
      display: none;
    }
    .el-radio {
      display: flex;
      align-items: center;
      margin: 5px 0;
      .address-item {
        span {
          margin-right: 10px;
        }
      }
    }
  }
}
.add-address-btu {
  padding-left: 20px;
}

.on-time {
  font-size: 10px;
}

.delivery-label {
  display: flex;
  justify-content: end;
  align-items: flex-end;
}
::v-deep  .el-form-item--small .el-form-item__label {
  display: flex;
  justify-content: flex-end;
}

.extract-map {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .map-show {
    cursor: pointer;
  }
}

.tip-box {
  .content {
    line-height: 25px;
    .color-red {
      color: red;
    }
  }
}
</style>
